package com.wj.facedetect.commom;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.support.multidex.MultiDex;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.wj.facedetect.service.PollingService;
import com.wj.facedetect.util.CrashHandlerUtil;
import com.wj.facedetect.util.PreferencesUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by wujing on 2017/9/7.
 */

public class FaceDetectApplication extends BaseApplication {
    private static final String TAG = FaceDetectApplication.class.getName();
    private static Context context;
    private List<Activity> mActivityList = new LinkedList<Activity>();
    //每个请求的tag标识
    private static long requestIndex = 0;
    private static String ANDROID_ID;

    public long getRequestIndex() {
        return requestIndex++;
    }

    @Override
    public void onCreate() {
        MultiDex.install(this);
        super.onCreate();
        context = this;
        Instance = this;
        CrashHandlerUtil.getInstance().init();
        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .connectTimeout(120000L, TimeUnit.MILLISECONDS)
                .readTimeout(120000L, TimeUnit.MILLISECONDS)
                .build();
        OkHttpUtils.initClient(okHttpClient);
        Fresco.initialize(this);
        ANDROID_ID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    private String getAppName(int pID) {
        String processName = null;
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        PackageManager pm = this.getPackageManager();
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
            try {
                if (info.pid == pID) {
                    processName = info.processName;
                    return processName;
                }
            } catch (Exception e) {
                // Log.d("Process", "Error>> :"+ e.toString());
            }
        }
        return processName;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onApplicationForeground() {
        //取出持久化token赋值给缓存
    }

    @Override
    public void onApplicatoinBackground() {
        //将缓存token持久化
    }


    private static FaceDetectApplication Instance;


    public static FaceDetectApplication getApplication() {
        if (Instance == null) {
            Instance = new FaceDetectApplication();
        }
        return Instance;
    }

    public static Context getContext() {
        return context;
    }

    /**
     * 添加activity
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (!mActivityList.contains(activity)) {
            mActivityList.add(activity);
        }
    }

    public static String getAndroidId() {
        return ANDROID_ID;
    }

    /**
     * 移除activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        mActivityList.remove(activity);
    }

    /**
     * 关闭所有activity
     */
    public void finishAllActivity() {
        //修改由于java.util.ConcurrentModificationException at java.util.LinkedList 到
        ArrayList<Activity> activityListTemp = new ArrayList<>();
        for (Iterator it = mActivityList.iterator(); it.hasNext(); ) {
            Activity activity = (Activity) it.next();
            activityListTemp.add(activity);
        }
        for (Activity activity : activityListTemp) {
            if (activity != null) {
                activity.finish();
            }
        }
    }

    /**
     * 突出程序
     */
    public void exit() {
        PreferencesUtils.getInstance().putSharePre(PreferencesUtils.IS_LOGIN, false);
        stopServices();
        finishAllActivity();
    }

    /**
     * 停止服务
     */
    public void stopServices() {
        //PushService.actionStop(this);
        stopService(new Intent(this, PollingService.class));
    }


}
